package com.syq.web;

import com.syq.bean.Users;
import com.syq.dao.impl.UsersDaoImpl;
import com.syq.service.UsersService;
import com.syq.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、接收参数
        String userName = req.getParameter("userName");
        String userPassword = req.getParameter("userPassword");
        //调取service
        UsersService usersService = new UsersServiceImpl();
        Users user = usersService.login(userName, userPassword);
        //跳转
        if(user==null) {
            //弹窗
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='login.jsp';alert('用户名或密码不正确')</script>");
        }else {
            //跳转主页面
            req.getSession().setAttribute("user",user);
            resp.sendRedirect("index.jsp");
        }
    }
}
